Git error: Your local changes to the following files would be overwritten by checkout

When you get the Git checkout error, “Your local changes to the following files would be overwritten by checkout,” one likely cause is that files in the master branch are indeed newer than the files in your feature branch.

But another possibility that I just learned about is that you did a git add, but forgot to do a git commit before trying to switch branches. My current wrong/accidental workflow looks like this:

$ git status (shows some files I changed)

$ git add .

$ (forgot `git commit` here)

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
    _overviews/hello-scala/case-objects.md
    _overviews/hello-scala/prelude-taste-of-scala.md
    _overviews/hello-scala/scala-features.md
    _overviews/hello-scala/scala-repl.md
Please commit your changes or stash them before you switch branches.
Aborting

As shown, I tried to do a git checkout master before I did a git commit, and so I got this error message. Therefore, the way to fix the problem is to do a git commit and then do the git checkout master:

$ git commit -m "some commit message"

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

So, if you ever run into the Git checkout error, “Your local changes to the following files would be overwritten by checkout,” I hope these notes are helpful.